package com.congapp.conexion;

import android.os.AsyncTask;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class ConexionServidor {

    private JSONObject obtenerJSONDesdeUrl(String url, List<NameValuePair> params) {
        JSONObject jsonObject = null;
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream inputStream = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }
            inputStream.close();
            String json = stringBuilder.toString();
            jsonObject = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSONException", e.getMessage(), e);
        } catch (ClientProtocolException e) {
            Log.e("ClientProtocolException", e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            Log.e("UnsupportedEncodingException", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("IOException", e.getMessage(), e);
        }
        return jsonObject;
    }

    public JSONObject obtenerJSON(String url, List<NameValuePair> parametros) {
        JSONObject jsonObject = null;
        try {
            Parametro parametro = new Parametro(url, parametros);
            Peticion peticion = new Peticion();
            jsonObject = peticion.execute(parametro).get();
        } catch (InterruptedException e) {
            Log.e("InterruptedException", e.getMessage(), e);
        } catch (ExecutionException e) {
            Log.e("ExecutionException", e.getMessage(), e);
        }
        return jsonObject;
    }

    private class Parametro {
        private String url;
        private List<NameValuePair> parametros;

        Parametro(String url, List<NameValuePair> parametros) {
            this.url = url;
            this.parametros = parametros;
        }
    }

    private class Peticion extends AsyncTask<Parametro, String, JSONObject> {

        @Override
        protected JSONObject doInBackground(Parametro... args) {
            ConexionServidor request = new ConexionServidor();
            JSONObject jsonObject1 = request.obtenerJSONDesdeUrl(args[0].url, args[0].parametros);
            return jsonObject1;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            super.onPostExecute(json);
        }
    }
}